package dev2dev.textclient.voip;

import gov.nist.javax.sip.stack.SIPTransactionStack;

import javax.sip.*;
import javax.sip.address.AddressFactory;
import javax.sip.header.HeaderFactory;
import javax.sip.message.MessageFactory;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jinbing.yao
 * @createTime 2023-10-30 19:42
 **/
public class MultiIPListenerExample {
    public static void main(String[] args) {
        int port = 5060; // 要监听的端口号
        String[] ips = {"127.0.0.1", "192.168.0.100"}; // 要监听的IP地址

        try {
            SipFactory sipFactory = SipFactory.getInstance();
            sipFactory.setPathName("gov.nist");

            SipStack sipStack = sipFactory.createSipStack(null);
            AddressFactory addressFactory = sipFactory.createAddressFactory();
            HeaderFactory headerFactory = sipFactory.createHeaderFactory();
            MessageFactory messageFactory = sipFactory.createMessageFactory();

            List<SipProvider> sipProviders = new ArrayList<>();
            Map<String,MySipListener> sipListenerList = new HashMap<>();
            for (String ip : ips) {

                InetAddress address = InetAddress.getByName(ip);
                ListeningPoint listeningPoint = sipStack.createListeningPoint(address.getHostAddress(), port, "udp");
                SipProvider sipProvider = sipStack.createSipProvider(listeningPoint);
                MySipListener mySipListener = new MySipListener();
                sipProvider.addSipListener(mySipListener);
                sipListenerList.put(ip,mySipListener);
                sipProviders.add(sipProvider);

                System.out.println("正在监听 " + address.getHostAddress() + ":" + port + "...");
            }

            // 在这里可以执行其他操作


            // 关闭监听器
            for (SipProvider sipProvider : sipProviders) {
                if (sipProvider.getListeningPoint().getIPAddress().equals("ip")) {
                    sipProvider.removeSipListener(sipListenerList.get("ip"));
                    break;
                }
            }

            // 关闭SIP栈
            ((SIPTransactionStack) sipStack).stopStack();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class MySipListener implements SipListener {
        @Override
        public void processRequest(RequestEvent requestEvent) {

        }

        @Override
        public void processResponse(ResponseEvent responseEvent) {

        }

        @Override
        public void processTimeout(TimeoutEvent timeoutEvent) {

        }

        @Override
        public void processIOException(IOExceptionEvent ioExceptionEvent) {

        }

        @Override
        public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {

        }

        @Override
        public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {

        }
        // 实现您的自定义SIP监听器逻辑
        // ...
    }
}
